package com.da.lon.wang.xlg.imgupload;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.activity.HomeActivity;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.DisplayImageOptions.Builder;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.List;

public class ImageGridAdapter extends ArrayAdapter<ImageInfo> {

    public final float DENSITY = Resources.getSystem().getDisplayMetrics().density;
    private LayoutInflater inflater;
    private List<String> imgs;
    private Context context;
    private int screenWidth;
    private LayoutParams params;
    private List<ImageInfo> infos;
    private DisplayImageOptions options;

    public ImageGridAdapter(Context context, List<ImageInfo> infos) {
        super(context, 0, infos);
        this.context = context;
        this.infos = infos;
        inflater = LayoutInflater.from(context);
        DisplayMetrics dm = new DisplayMetrics();

        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);

        screenWidth = dm.widthPixels;
        params = new LayoutParams((screenWidth - dipToPixel(3) * 3 - 10) / 4,
                (screenWidth - dipToPixel(3) * 3 - 10) / 4);
        Builder build = new DisplayImageOptions.Builder();
        build.cacheInMemory();
        build.imageScaleType(ImageScaleType.EXACTLY_STRETCHED);
        build.bitmapConfig(Bitmap.Config.RGB_565);
        build.showImageForEmptyUri(R.drawable.xlg_picture_default);
        build.showImageOnFail(R.drawable.xlg_picture_default);
        options = build.build();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHoler holder = null;
        try {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.xlg_image_grid_item, null);
                holder = new ViewHoler();
                holder.imageView = (ImageView) convertView.findViewById(R.id.image);
                holder.selectedImage = (ImageView) convertView.findViewById(R.id.selected);
                convertView.setTag(holder);
            } else {
                holder = (ViewHoler) convertView.getTag();
            }
            holder.imageView.setLayoutParams(params);

            final ImageInfo info = getItem(position);

            final ImageView tempImage = holder.selectedImage;
            if (info.isSelected) {
                tempImage.setVisibility(View.VISIBLE);
            } else {
                tempImage.setVisibility(View.GONE);
            }

            holder.imageView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    // ChooseAvtivity.selectUri="file://" + info.path;
                    if (info.isSelected) {
                        tempImage.setVisibility(View.GONE);
                        info.isSelected = false;
                        MyApplication.getInstance().deleteImageInfo(info.path);
                    } else {
                        if (MyApplication.getInstance().getImageInfosSize() >= HomeActivity.uploadMaxNum) {
                            Toast.makeText(context, "最多只能选" + HomeActivity.uploadMaxNum + "张", Toast.LENGTH_LONG).show();
                        } else {
                            tempImage.setVisibility(View.VISIBLE);
                            info.isSelected = true;
                            MyApplication.getInstance().addImageInfo(info);
                            PreferenceUtils.getInstance().SetSettingString("head_path", info.path);
                        }
                    }
                }
            });
            ImageLoader.getInstance().displayImage("file://" + info.path, holder.imageView, options);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return convertView;
    }

    public int dipToPixel(int dip) {
        return (int) (dip * DENSITY + 0.5f);
    }

    public float pixelToDip(float px) {
        return (px - 0.5f) / DENSITY;
    }

    private class ViewHoler {
        ImageView imageView;
        ImageView selectedImage;
        TextView textView;
    }

}
